package com.seu.me.test.api;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.springframework.web.client.RestTemplate;

/**
 * 高德经纬度、地址信息接口API
 *
 * @author liangfeihu
 * @since 2019/8/13
 */
public class GaodeMapApi {
    private static final String key = "";
    private static final String geoUrl = "https://restapi.amap.com/v3/geocode/geo?key=2f75be96c128b23b37077fb97e9791b1&output=JSON";
    private static final String regeoUrl = "https://restapi.amap.com/v3/geocode/regeo?&key=2f75be96c128b23b37077fb97e9791b1&output=JSON&radius=1000&extensions=base";

    private static final String locateWifiUrl = "http://apilocate.amap.com/position?&key=5352fa91f26be7c438aa8a6bde53b4f8&output=json&accesstype=1&imei=352315052834187" +
            "&smac=E0:DB:55:E4:C7:49&mmac=50:a7:2b:1e:56:58,-60,alibaba-inc&macs=4c:48:da:25:0b:11,-59,alibaba-inc|4c:4\n" +
            "8:da:25:1a:11,-77,alibaba-inc&serverip=10.2.166.4";

    private static final String locateUrl = "http://apilocate.amap.com/position?&key=5352fa91f26be7c438aa8a6bde53b4f8&output=json&accesstype=0&cdma=0&" +
            "bts=460,0,4189,8869,-63";

    private static final String locateUrl2 = "http://apilocate.amap.com/position?&key=5352fa91f26be7c438aa8a6bde53b4f8&output=json&accesstype=0&imei=352315052834187&" +
            "cdma=1&bts=13824,1,1838,1674723,575739,-52";

    public static void main(String[] args) {
        System.out.println(String.format("&location=%s,%s", "115.320974", "36.189343"));
        // 根据地理位置获取经纬度信息api
        //testGeo();
        // 根据经纬度获取地理位置信息api
        //testRegeo();
        // 根据手机的一些信息获取精确定位api
        testLocate();
    }

    public static void testLocate() {
        RestTemplate restTemplate = new RestTemplate();
        JSONObject object = restTemplate.getForObject(locateUrl, JSONObject.class);
        // JSONObject object = restTemplate.getForObject(locateWifiUrl, JSONObject.class);
        System.out.println(object.toJSONString());
        Integer status = object.getInteger("status");
        if (status != null && status == 1) {
            System.out.println("请求定位api成功了");
            JSONObject result = object.getJSONObject("result");
            Integer type = result.getInteger("type");
            if (type != null && type != 0) {
                String desc = result.getString("desc");
                System.out.println("地址信息：formatted_address=" + desc);
                String location = result.getString("location");
                String[] split = location.split(",");
                System.out.println("经纬度信息：经度=" + split[0] + " 维度=" + split[1]);
            } else {
                System.out.println("没有得到定位结果: " + result.toJSONString());
            }
        } else {
            System.out.println("请求定位api失败了: " + object.getString("info"));
        }
    }

    public static void testRegeo() {
        RestTemplate restTemplate = new RestTemplate();
        // String location = "&location=116.310003,39.991957";
        // String location = "&location=121.483216,31.289433";
        String location = "&location=115.320974,36.189343";
        JSONObject object = restTemplate.getForObject(regeoUrl + location, JSONObject.class);
        System.out.println(object.toJSONString());
        Integer status = object.getInteger("status");
        if (status != null && status == 1) {
            System.out.println("请求地址信息成功了");
            JSONObject geocode = object.getJSONObject("regeocode");
            String formattedAddress = geocode.getString("formatted_address");
            System.out.println("地址信息：formatted_address=" + formattedAddress);
        } else {
            System.out.println("请求地址信息失败: " + object.getString("info"));
        }
    }

    public static void testGeo() {
        RestTemplate restTemplate = new RestTemplate();
        // String address = "&address=北京市朝阳区阜通东大街6号";
        // String address = "&address=上海市虹口区大柏树地铁站";
        String address = "&address=河北省邯郸市大名县埝头乡前沙路村";
        JSONObject object = restTemplate.getForObject(geoUrl + address, JSONObject.class);
        System.out.println(object.toJSONString());
        Integer status = object.getInteger("status");
        if (status != null && status == 1) {
            System.out.println("请求经纬度成功了");
            JSONArray geocodes = object.getJSONArray("geocodes");
            if (geocodes != null & geocodes.size() > 0) {
                JSONObject geoObject = geocodes.getJSONObject(0);
                String location = geoObject.getString("location");
                String[] split = location.split(",");
                System.out.println("经纬度信息：经度=" + split[0] + " 维度=" + split[1]);
            } else {
                System.out.println("返回数据为空~");
            }

        } else {
            System.out.println("请求经纬度失败: " + object.getString("info"));
        }
    }

}
